import 'package:get/get.dart';
import 'package:whale/pages/im/component/im_input/index.dart';
import 'package:whale/pages/im/im/index.dart';

import 'index.dart';

class ImInputExpressionController extends GetxController {
  ImInputExpressionController();
  final inputController = Get.find<ImInputController>();

  final state = ImInputExpressionState();
  void actionLastItem(String name) {
    var item = state.data[name]!;
    if (state.lastNames!.contains(name)) {
      state.lastNames!.remove(name);
      state.lastItems!.remove(item);
    }

    state.lastNames!.insert(0, name);
    state.lastItems!.insert(0, item);
    if (state.lastNames!.length > 8) {
      state.lastNames!.removeRange(8, state.lastNames!.length);
      state.lastItems!.removeRange(8, state.lastItems!.length);
    }
    update(['last']);

    inputController.getEmoji(name, item);
  }

  void actionGridItem(int idx) {
    var name = state.names[idx];
    state.lastNames ??= [];
    state.lastItems ??= [];

    actionLastItem(name);
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    final im = Get.find<ImController>().state;
    state.names = im.emojiNames;
    state.data = im.emojiData;
    state.items = state.names.map((e) => state.data[e]!).toList();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
